package com.shengqi.xinnongren.work.ui.user.presenter;

import com.google.gson.Gson;
import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.common.md5.SafeUtils;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.user.model.MessageBean;
import com.shengqi.xinnongren.work.ui.user.view.MessageView;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/6/27.
 */
public class MessagePresenter extends BasePresenter<MessageView> {
    public void getMessageData() {
        getView().showLoadingView();
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), Constants.getJsonData(null, UserManager.getInstance().getToken()).toString());
        addTask(RetrofitHelper.getInstance().getService().getMessageData(requestBody), respond -> {
            getView().hideLoadingView();
            HttpRespond<String> httpResult = Global.getHttpResult(respond);
            if (httpResult.result == 1) {
                MessageBean bean = new Gson().fromJson(httpResult.data, MessageBean.class);
                getView().showMessageData(bean);
            } else if (httpResult.result == -1) {
                getView().loginInvalid();
            } else
                getView().onRequestFailed(httpResult.message);

        });
    }
}
